﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BL;

namespace QuanLyCHTrangSuc
{
    public partial class frmChiTietDonDatHang : Form
    {
        List<SanPhamDAL> dsSanPham = new List<SanPhamDAL>();
        SanPhamBL sp = new SanPhamBL();
        DonDatHangDAL donHang = new DonDatHangDAL();
        int maDH;
        int d;

        List<ChiTietDonDatHangDAL> dsChiTietDDH = new List<ChiTietDonDatHangDAL>();
        ChiTietDonDatHangBL ctDDH = new ChiTietDonDatHangBL();
        ChiTietDonDatHangDAL ChiTietDDH = new ChiTietDonDatHangDAL();
        public frmChiTietDonDatHang()
        {
            InitializeComponent();
        }
        public frmChiTietDonDatHang(int i)
        {
            this.maDH = i;
            InitializeComponent();

        }
        //load ds san pham len combobox
        void LoadDSSanPham()
        {
            dsSanPham = sp.LayDanhSachSanPham();
            cboTenSP.DataSource = dsSanPham;
            cboTenSP.ValueMember = "MaSanPham";
            cboTenSP.DisplayMember = "TenSanPham";
        }

        private void LayThongTin()
        {
            ChiTietDDH.MaDonDatHang = int.Parse(txtMaDDH.Text);
            ChiTietDDH.MaSanPham = cboTenSP.SelectedValue.ToString();
            ChiTietDDH.SoLuong = int.Parse(txtSoLuong.Text.ToString());
            ChiTietDDH.ThanhTien = float.Parse(txtThanhTien.Text.ToString());


        }

        private void LoadChiTietDDH()
        {

            txtMaDDH.Text = maDH.ToString();
            //load len gridview
            dsChiTietDDH = ctDDH.LayDSChiTietDonDatHang(maDH);
            if (dsChiTietDDH.Count != 0)
            {
                for (int i = 0; i < dsChiTietDDH.Count; i++)
                {
                    this.gridChiTietDDH.Rows.Add();
                    this.gridChiTietDDH.Rows[i].Cells[0].Value = i + 1;
                    this.gridChiTietDDH.Rows[i].Cells[1].Value = dsChiTietDDH[i].MaSanPham;
                    this.gridChiTietDDH.Rows[i].Cells[2].Value = sp.LaySanPhamTheoMaSP(dsChiTietDDH[i].MaSanPham).TenSanPham;
                    this.gridChiTietDDH.Rows[i].Cells[3].Value = dsChiTietDDH[i].SoLuong;
                    this.gridChiTietDDH.Rows[i].Cells[4].Value = dsChiTietDDH[i].ThanhTien;
                }
            }
        }

        private void frmChiTietDonDatHang_Load(object sender, EventArgs e)
        {
            LoadDSSanPham();
            LoadChiTietDDH();

            
        }



        
        private void gridChiTietDDH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            d = e.RowIndex;
            //MessageBox.Show(d.ToString());

            //khong lay du lieu rong
            if (d == this.gridChiTietDDH.Rows.Count-1)
                return;
            //MessageBox.Show(i.ToString());
            txtSoLuong.Text = this.gridChiTietDDH.Rows[d].Cells[3].Value.ToString();
            txtThanhTien.Text = this.gridChiTietDDH.Rows[d].Cells[4].Value.ToString();
            cboTenSP.Text = this.gridChiTietDDH.Rows[d].Cells[2].Value.ToString();

            txtDonGia.Text = sp.LaySanPhamTheoMaSP(this.gridChiTietDDH.Rows[d].Cells[1].Value.ToString()).GiaBan.ToString();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(d.ToString());            
            string ma = this.gridChiTietDDH.Rows[d].Cells[1].Value.ToString();           
            LayThongTin();
            if (ctDDH.CapNhatChiTietDonDatHang(ChiTietDDH, ma))
            {
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            gridChiTietDDH.Rows.Clear();
            LoadChiTietDDH();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtDonGia.Text = "";
            txtSoLuong.Text = "";
            txtThanhTien.Text = "";

            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(gridChiTietDDH.Rows.Count.ToString());
            string maDDH= txtMaDDH.Text;
            string ma1 = cboTenSP.SelectedValue.ToString();
            //string ma2 = this.gridChiTietDDH.Rows[d].Cells[1].Value.ToString();
            if (gridChiTietDDH.Rows.Count == 1  )
                MessageBox.Show("Không có dữ liệu để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if(ma1 != this.gridChiTietDDH.Rows[d].Cells[1].Value.ToString())
                MessageBox.Show("Không có dữ liệu để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                DialogResult Xoa = MessageBox.Show("Bạn có chắc muốn xóa chi tiết đơn hàng này?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (Xoa == DialogResult.Yes)
                {
                    ChiTietDonDatHangBL.XoaChiTietDonDatHang(maDDH, this.gridChiTietDDH.Rows[d].Cells[1].Value.ToString());
                    gridChiTietDDH.Rows.Clear();
                    LoadChiTietDDH();
                    

                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            string sl = txtSoLuong.Text;
            string ttien = txtThanhTien.Text;
            if (sl == "" || ttien == "")
                MessageBox.Show("Bạn chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            LayThongTin();
            if (ctDDH.ThemChiTietDonDatHang(ChiTietDDH))
            {
                MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            gridChiTietDDH.Rows.Clear();
            LoadChiTietDDH();

        }

        private void cboTenSP_SelectedValueChanged(object sender, EventArgs e)
        {
            txtDonGia.Text = sp.LaySanPhamTheoMaSP(cboTenSP.SelectedValue.ToString()).GiaBan.ToString();
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            if (txtSoLuong.Text != "")
            {
                int gia=int.Parse(txtDonGia.Text.ToString());
                int sl = int.Parse(txtSoLuong.Text.ToString());
                txtThanhTien.Text = (gia * sl).ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
